package org.finmanager.core;

import com.alee.laf.button.WebButton;

import javax.swing.*;
import javax.swing.table.TableColumn;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static org.finmanager.core.Constants.CATEGORY_MANAGER_NAME;
import static org.finmanager.core.Constants.CATEGORY_MANAGER_WIDTH;
import static org.finmanager.core.Constants.CATEGORY_MANAGER_HEIGHT;
import static org.finmanager.core.Constants.CATEGORY_MANAGER_RESIZABLE;

/**
 * Created by Kelvin on 22/3/2014.
 * <p/>
 * A place for adding/deleting categories and setting favourite for a category
 * <p/>
 * Editing a category is not (yet) allowed
 */

//Tpying the "Cat" button in the budget function will pop up this 
public class CategoryManagerUI {
	private JPanel mainPanel = new JPanel();

	private JFrame frame;

	private JTable table;

	private WebButton addCategoryButton = new WebButton("+");

	private CategoryPopUpMenu categoryPopUpMenu = new CategoryPopUpMenu();

	// A table is created for displaying categories
	// User can directly set favourite
	public CategoryManagerUI(CategoryManagerController categoryManagerController) {
		table = new JTable(categoryManagerController.getCategoryModel().getCategoryTableModel());
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.DARK_GRAY));

		// The favourite column is narrower
		TableColumn column;
		for (int i = 0; i < table.getColumnCount(); ++i) {
			column = table.getColumnModel().getColumn(i);
			if (i == CategoryTableModel.NAME)
				column.setPreferredWidth(250);
			if (i == CategoryTableModel.FILTER)
				table.removeColumn(column);
		}
		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setColumnSelectionAllowed(false);

		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);

		table.setShowGrid(false);
		table.setIntercellSpacing(new Dimension(0,0));

		table.setComponentPopupMenu(categoryPopUpMenu);

		table.addMouseListener(new TableMouseAdapter(table));


		JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

		addCategoryButton.setDrawFocus(false);
		addCategoryButton.setActionCommand(CategoryManagerController.OPEN_CATEGORY_INPUT);

		topPanel.add(addCategoryButton);

		getMainComponent().setLayout(new BorderLayout(0, 0));
		getMainComponent().add(scrollPane, BorderLayout.CENTER);
		getMainComponent().add(topPanel, BorderLayout.NORTH);

		createAndShowGUI(categoryManagerController.getItemManagerController().getUIMainComponent());
	}

	public void buttonActionListener(ActionListener listener) {
		addCategoryButton.addActionListener(listener);
		categoryPopUpMenu.addMenuItemListener(listener);
	}

	public JComponent getMainComponent() {
		return mainPanel;
	}

	private void createAndShowGUI(JComponent relative) {
		frame = new JFrame(CATEGORY_MANAGER_NAME);
		frame.setSize(new Dimension(CATEGORY_MANAGER_WIDTH, CATEGORY_MANAGER_HEIGHT));
		frame.setLocationRelativeTo(relative);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setResizable(CATEGORY_MANAGER_RESIZABLE);

		frame.add(getMainComponent());

		frame.setVisible(true);
	}

	public int getSelectedRow() {
		return table.getSelectedRow();
	}

	// Select a row when right clicking it
	private class TableMouseAdapter extends MouseAdapter {

		private JTable table;

		public TableMouseAdapter(JTable table) {
			this.table = table;
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.getButton() == MouseEvent.BUTTON3) {
				Point point = e.getPoint();
				int currentRow = table.rowAtPoint(point);
				if (currentRow != -1)
					table.setRowSelectionInterval(currentRow, currentRow);
			}
		}
	}
}
